package com.example.springmvcdemo;

import com.example.springmvcdemo.model.Person;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/*
 * @author zzr
 * @date: 2025/09/14  10:39
 * @description:
 */
@RestController
public class TestForParamController {
    @RequestMapping("/m1")
    public String m1(String name){
        return name;
    }

    @RequestMapping("/m2")
    public String m2(Person p) {
        return p.toString();
    }

    @RequestMapping("/m3")
    public String m3(@RequestParam(value = "time", required = false) String timming) {
        return timming;
    }

    @RequestMapping("/m4")
    public String m4(String[] array) {
        return Arrays.toString(array);
    }

    @RequestMapping("/m5")
    public List<String> m5(@RequestParam List<String> listParam) {
        return listParam;
    }

    @RequestMapping("/m6/{id}/{name}")
    public String m6(@PathVariable Integer id, @PathVariable("name")String username) {
        return "" + id + username;
    }

    @RequestMapping("/m7")
    public String getFile(@RequestPart MultipartFile file) {
        // 获取文件名称
        String fileName = file.getOriginalFilename();
        return fileName;
    }

    @RequestMapping("/m8")
    public String Cookie(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        StringBuilder builder = new StringBuilder();
        if (cookies != null) {
            for (Cookie ck:cookies) {
                builder.append(ck.getName() + ck.getValue());
            }
        }
        return builder + "Cookie";
    }
    @RequestMapping("/m9")
    public String Cookie2(@CookieValue("bite") String bite) {
        return bite + "";
    }

    @RequestMapping("/m10")
    public String setSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (null != session) {
            session.setAttribute("username","java");
        }
        return session + "";
    }
    @RequestMapping("/m11")
    public String getSession(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        String username = null;
        if (session != null && session.getAttribute("username") != null) {
            username = (String) session.getAttribute("username");
        }
        return username;
    }
    @RequestMapping("/m12")
    public String getSession2(@SessionAttribute(value = "username", required = false) String username) {
        return username;
    }

    @RequestMapping("/m13")
    public String getSession3(HttpSession session) {
        String username = (String) session.getAttribute("username");
        return username;
    }

    @RequestMapping("/m14")
    public String getHeader(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        return userAgent;
    }

    @RequestMapping("/m15")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent) {
        return userAgent ;
    }

    @RequestMapping("/m16")
    public HashMap<String,String> returnJson() {
        HashMap<String,String> map = new HashMap<>();
        map.put("Java","JavaValue");
        map.put("Mysql","MysqlValue");
        map.put("PHP","PHPValue");
        return map;
    }
    @RequestMapping("/m17")
    public String setStatues(HttpServletResponse response) {
        response.setStatus(401);
        return "成功";
    }

    @RequestMapping(value = "/m18",produces = "application/json")
    public String setContentType(){
        return "成功";
    }
}
